﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Tayseer.DAL
{
   public  class Case_MembersEntity
    {
             /// Id Case_Id Mem_Id  Mem_Type

       public Int32 id { get; set; }
       public Int32 Case_Id { get; set; }
       public Int32 Mem_Id { get; set; }
       public String Mem_Type { get; set; }
       
    }
   public class Case_Members
   {
       //private static String conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='Tayseer.accdb' ;Jet OLEDB:Database Password=Yarabe11";
       private static string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (System.Reflection.Assembly.GetExecutingAssembly().Location).Remove((System.Reflection.Assembly.GetExecutingAssembly().Location).IndexOf("Tayseer.exe")) + "Tayseer.accdb;Jet OLEDB:Database Password=Yarabe11";
       private static String cmdText = "";

       public static Boolean insert(Case_MembersEntity m)
       {
           bool flag = true;

           cmdText = "INSERT INTO Case_Members (Case_Id,Mem_Id,Mem_Type) values (?,?,?)";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@Case_Id", m.Case_Id);
           cmd.Parameters.AddWithValue("@Mem_Id", m.Mem_Id);
           cmd.Parameters.AddWithValue("@Mem_Type", m.Mem_Type);

           try
           {
               con.Open();
               Int32 x = cmd.ExecuteNonQuery();
               if (x == 0)
               {
                   flag = false;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("error" + ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }

           }
           return flag;

       }
       public static Boolean update(Case_MembersEntity m)
       {
           bool flag = true;

           cmdText = "Update Case_Members set Case_Id=? ,Mem_Id= ?,  Mem_Type=? where Id =?";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@Case_Id", m.Case_Id);
           cmd.Parameters.AddWithValue("@Mem_Id", m.Mem_Id);
           cmd.Parameters.AddWithValue("@Mem_Type", m.Mem_Type);
           cmd.Parameters.AddWithValue("@Id", m.id);

           try
           {
               con.Open();
               Int32 x = cmd.ExecuteNonQuery();
               if (x == 0)
               {
                   flag = false;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("error" + ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }

           }
           return flag;

       }
       public static Boolean Delete(int id)
       {
           bool flag = true;

           cmdText = "delete from Case_Members where Id= ?  ";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@Id", id);

           try
           {
               con.Open();
               Int32 x = cmd.ExecuteNonQuery();
               if (x == 0)
               {
                   flag = false;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("error" + ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }

           }
           return flag;

       }
       public static Case_MembersEntity getById(Int32 id)
       {
           Case_MembersEntity m = null;
           cmdText = "Select * from Case_Members where Id = ?";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@Id", id);

           try 
           {
               con.Open();
               OleDbDataReader reader = cmd.ExecuteReader();
               m = FillEntityFromReader(reader);
               reader.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Case_MembersEntity error" + ex.Message);
           }
           finally
           {
                     if (con.State == ConnectionState.Open)
                    con.Close();
           }
           return m;
       }
       public static List <Case_MembersEntity> getAll( )
       {
           Case_MembersEntity c;
           List<Case_MembersEntity> m = new List<Case_MembersEntity>();
           cmdText = "Select * from Case_Members ";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);



           try
           {
               con.Open();
               OleDbDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
               {
                   c = FillEntityFromReader(reader);
                   m.Add(c);
               }
               reader.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Get all Case_MembersEntity error" + ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
                   con.Close();
           }
           return m;
       }
       public static Case_MembersEntity FillEntityFromReader(OleDbDataReader reader)
       {

           Case_MembersEntity m = new Case_MembersEntity();
           m.id = Int32.Parse(reader["Id"].ToString());
           if (reader["Mem_Id"] != null)
               m.Mem_Id = Int32.Parse(reader["Mem_Id"].ToString());
           if (reader["Case_Id"] != null)
               m.Case_Id = Int32.Parse(reader["Case_Id"].ToString());
           if (m.Mem_Type != null)
               m.Mem_Type = reader["Mem_Type"].ToString();
           return m;
       }
   }
}
